# Prompt ideas freely used and stolen from:
# ---
# * http://blog.bigdinosaur.org/easy-ps1-colors/
# * https://github.com/eprev/dotfiles/blob/master/includes/prompt.bash
# * http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html
# * https://github.com/mathiasbynens/dotfiles/blob/master/.bash_prompt
# * https://github.com/sindresorhus/pure
# * http://ebeab.com/2014/04/17/the-bash-prompt/
#
color_prompt=0
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    *color*) color_prompt=1;;
    rxvt-unicode) color_prompt=1;;
esac

RED=''
CYAN=''
GREEN=''
MAGENTA=''
ORANGE=''
PURPLE=''
WHITE=''
LIME_YELLOW=''
POWDER_BLUE=''
RESET=''

if (( color_prompt > 0 )); then
    RED=$(tput setaf 1)
    CYAN=$(tput setaf 6)
    GREEN=$(tput setaf 2)
    MAGENTA=$(tput setaf 9)
    ORANGE=$(tput setaf 172)
    PURPLE=$(tput setaf 141)
    WHITE=$(tput setaf 254)
    LIME_YELLOW=$(tput setaf 190)
    POWDER_BLUE=$(tput setaf 153)
    RESET=$(tput sgr0)
fi

_rprompt() {
  local _time=$1
  (( $_time < 5 )) && return
  local _out
  local days=$(( $_time / 60 / 60 / 24 ))
  local hours=$(( $_time / 60 / 60 % 24 ))
  local minutes=$(( $_time / 60 % 60 ))
  local seconds=$(( $_time % 60 ))
  (( $days > 0 )) && _out="${days}d"
  (( $hours > 0 )) && _out="$_out ${hours}h"
  (( $minutes > 0 )) && _out="$_out ${minutes}m"
  _out="$_out ${seconds}s"
  printf "${RED}$_out${RESET}"
}

exit_code_emote() {
    case $1 in
        # 0)   prompt="(•◡•)❥";; # success
        0) prompt="(/^ヮ^)/*:・ﾟ✧";; # success
        127) prompt="(•ิ_•ิ)❥" ;; # command not found
        130) prompt="(☉_☉)❥" ;; # ^C
        # 148) prompt="ᕕ( ᐛ )ᕗ" ;;
        148) prompt="(°▽°)❥" ;; # ^z
        *)   prompt="(•́‸•̀)❥" ;; # Other errors
        # *)   prompt="(•̀д•́)❥" ;;
    esac
}

prompt() {
    local _exitcode=$?

    local _end_time=$(date +%s)
    local _total_time=$(( _end_time - _start_time ));
    if ((_end_time == _start_time || _start_time == 0)); then
      _total_time=0
    fi

    unset _start_time

    exit_code_emote $_exitcode

    if [ $_exitcode -eq 0 ]; then
      color=${GREEN}
    else
      color=${RED}
    fi

    venv=''
    if [ -n "$VIRTUAL_ENV" ]; then
        venv="(\[${BRIGHT}${MAGENTA}\]$(basename $VIRTUAL_ENV)\[${RESET}\])"
    fi

    GIT_PS1_SHOWCOLORHINTS=1
    GIT_PS1_SHOWUNTRACKEDFILES=1
    GIT_PS1_SHOWDIRTYSTATE=1
    GIT_PS1_SHOWUPSTREAM="auto verbose"
    . "$HOME"/bin/git-prompt.sh

    # __git_ps1 here is provided by ~/bin/git-prompt.sh
    _top_row="\[${CYAN}\]\W\[${RESET}\]$(__git_ps1 ' (%s)')\[${RESET}\]"
    [[ "$SSH_CONNECTION" != '' ]] && \
      _top_row="\[${BRIGHT}${MAGENTA}\]\u\[${RESET}\]@\[${ORANGE}\]\h\[${RESET}\]:${_top_row}"

    # _bottom_row="${venv}\[${color}\]❯\[${RESET}\] "
    _bottom_row="${venv}\[${color}\]${prompt}\[${RESET}\] "

    PS1="${_top_row}\$(_rprompt $_total_time)\n${_bottom_row}"
}

_log_start() {
  local hist

  [ -n "$COMP_LINE" ] && return  # do nothing if completing
  [[ "$BASH_COMMAND" == "$PROMPT_COMMAND" ]] && return # don't cause a preexec for $PROMPT_COMMAND

  hist=$(HISTTIMEFORMAT='%s ' history 1 | { read -r _ this_command; echo "$this_command"; })
  [[ -n "$hist" ]] && {
    # https://www.debian-administration.org/article/543/Bash_eternal_history
    echo "$$ $PWD $USER $hist $BASH_COMMAND" >> "$HOME/.muh_history"
  }

  _start_time=$(date +%s)
}

trap '_log_start' DEBUG
PROMPT_COMMAND=prompt
PS2="\[$ORANGE\]→ \[$RESET\]"
